package com.mapapp.flutter_mapapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mapapp.flutter_mapapp.R;

import java.util.List;

public class SearchResultAdapter extends RecyclerView.Adapter<SearchResultAdapter.SearchResultViewHolder>{

    private List<String> mAddresses;
    private Context mContext;

    public SearchResultAdapter(List<String> addresses , Context content){
        this.mAddresses = addresses;
        this.mContext = content;
    }

    public void notify(List<String> addresses){
        this.mAddresses = addresses;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SearchResultViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_result, parent, false);
        SearchResultViewHolder holder = new SearchResultViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull SearchResultViewHolder holder, final int position) {
        holder.address.setText(mAddresses.get(position));
        holder.linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null){
                    onItemClickListener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mAddresses == null) {
            return 0;
        }
        return mAddresses.size();
    }

    static class SearchResultViewHolder extends RecyclerView.ViewHolder {
        private TextView address;
        private LinearLayout linear;

        public SearchResultViewHolder(View itemView) {
            super(itemView);
            address = itemView.findViewById(R.id.tv_search_address);
            linear = itemView.findViewById(R.id.ll_item_search_result);
        }
    }

    //私有属性
    private OnItemClickListener onItemClickListener = null;

    //setter方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    //回调接口
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
